package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.jicai.view.adapter.CentralizedMiningChildAdapter;
import com.sgb.jicai.view.adapter.FactoryCentralizedListItemChildAdapter;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemCentralizedMining2Binding;
import com.sgb.kjwl.databinding.ItemFactoryCentralizedListBinding;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.sgb.jicai.entity.FactoryCentralizedItemEntity;
import com.sgb.kjwl.view.ui.activity.manage.ShopInfoForCompanyTradesActivity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;
import java.util.List;

/**
 * Time:2021/4/27
 * AUTHOR:F_Q
 * Description:
 */
public class ShopJiCaiAdapter<T>  extends BaseRecycleAdapter<T> {

    private boolean isVisible;
    private ItemClickCallBack itemCallBack;
    public ShopJiCaiAdapter(Context context, int layoutId, List<T> datas) {
        super(context, layoutId, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, T t) {
        if(t!=null&&t instanceof CentralizedMiningEntity){
            CentralizedMiningEntity entity = (CentralizedMiningEntity) t;
            holder.setBinding(BR.viewModel, entity);
            ItemCentralizedMining2Binding binding = (ItemCentralizedMining2Binding) holder.getmBinding();
            binding.ent.setVisibility(isVisible ? View.VISIBLE : View.GONE);
            binding.ent.setOnClickListener(view ->
                    ShopInfoForCompanyTradesActivity.startAct(context, entity.getSupplierId(), entity.getCompanyNo(), 3, 0));
            updateRecycler(((ItemCentralizedMining2Binding) holder.getmBinding()).childRecycler, entity);
            updateImageStatus(((ItemCentralizedMining2Binding) holder.getmBinding()).imgTab, entity);
            updateGroupBuyType(((ItemCentralizedMining2Binding) holder.getmBinding()).tvStatus, entity);
            updateSecondlyType(((ItemCentralizedMining2Binding) holder.getmBinding()).tvSecondlyType, entity);
            updateQuotationType(((ItemCentralizedMining2Binding) holder.getmBinding()).tvQuotationType, entity);
            ((ItemCentralizedMining2Binding) holder.getmBinding()).rlContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemCallBack != null) {
                        itemCallBack.onItemClick(1,entity.getNo());//可用此替换setOnItemClick
                    }
                }
            });
        }else if(t!=null&&t instanceof FactoryCentralizedItemEntity) {
            FactoryCentralizedItemEntity entity = (FactoryCentralizedItemEntity)t;
           holder.setBinding(BR.viewModel, entity);
            ItemFactoryCentralizedListBinding  itemViewHolder = (ItemFactoryCentralizedListBinding) holder.getmBinding();
            itemViewHolder.ent.setVisibility(isVisible ? View.VISIBLE : View.GONE);
            itemViewHolder.ent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ShopInfoForCompanyTradesActivity.class);
                    if (entity.getSupplierId() != null) {
                        intent.putExtra("id", entity.getSupplierId());
                        intent.putExtra("companyNo", entity.getCompanyNo());
                        intent.putExtra("position", 3);
                        intent.putExtra("childPosition", 0);
                        context.startActivity(intent);
                    } else {
                        MToast.showToast(context, "店铺id为空");
                    }
                }
            });

            updateRecyclertwo(((ItemFactoryCentralizedListBinding) holder.getmBinding()).childRecycler, entity);
            updateImageStatustwo(((ItemFactoryCentralizedListBinding) holder.getmBinding()).imgTab, entity);

            ((ItemFactoryCentralizedListBinding) holder.getmBinding()).rlContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemCallBack != null) {
                        itemCallBack.onItemClick(2,entity.getNo());//可用此替换setOnItemClick
                    }
                }
            });
        }
    }

    private void updateImageStatustwo(ImageView imageView, FactoryCentralizedItemEntity entity) {
        // 集采状态状态1 集采中 2 集采完成
        if (entity.getStatus() == 1) {
            switch (entity.getPriceWarning()) {//价格预警 1 大于 2 等于 3 小于
                case 1:
                    imageView.setImageResource(R.drawable.jcz_dy);
                    break;
                case 2:
                    imageView.setImageResource(R.drawable.jcz_dengyu);
                    break;
                case 3:
                    imageView.setImageResource(R.drawable.jcz_xy);
                    break;
                default:
                    imageView.setImageResource(R.drawable.jcz_dengyu);
                    break;
            }
        } else {
            imageView.setImageResource(R.drawable.icon_jcwc);
        }
    }

    private void updateRecyclertwo(RecyclerView recyclerView, FactoryCentralizedItemEntity entity) {
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        FactoryCentralizedListItemChildAdapter childAdapter = new FactoryCentralizedListItemChildAdapter(recyclerView.getContext(), entity.getProductSaleSlipDetails());
        recyclerView.setAdapter(childAdapter);
        childAdapter.setOnItemClick(new AdapterOnItemClick<FactoryCentralizedItemEntity.ProductSaleSlipDetails>() {
            @Override
            public void onItemClick(FactoryCentralizedItemEntity.ProductSaleSlipDetails productSaleSlipDetails, int position) {
                if (itemCallBack != null) {
                    itemCallBack.onItemClick(2,entity.getNo());//可用此替换setOnItemClick
                }
            }
        });
    }

    private void updateRecycler(RecyclerView recyclerView, CentralizedMiningEntity entity) {
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        CentralizedMiningChildAdapter childAdapter = new CentralizedMiningChildAdapter(recyclerView.getContext(), entity.getGoodsEntity());
        recyclerView.setAdapter(childAdapter);
        childAdapter.setOnItemClick(new AdapterOnItemClick<CentralizedMiningEntity.GoodsEntity>() {
            @Override
            public void onItemClick(CentralizedMiningEntity.GoodsEntity goodsEntity, int position) {//给内层添加点击事件，解决嵌套时点击内层无效果的问题
                if (itemCallBack != null) {
                    itemCallBack.onItemClick(1,entity.getNo());
                }
            }
        });
    }

    private void updateImageStatus(ImageView imageView, CentralizedMiningEntity entity) {
        // 集采状态（1 集采中 2 集采结束 4 集采失败 5 加盟中 6 报价中 ）
        switch (entity.getStatus()) {
            case 1:
                switch (entity.getPriceWarning()) {//价格预警 1 大于 2 等于 3 小于
                    case 1:
                        imageView.setImageResource(R.drawable.jcz_dy);
                        break;
                    case 2:
                        imageView.setImageResource(R.drawable.jcz_dengyu);
                        break;
                    case 3:
                        imageView.setImageResource(R.drawable.jcz_xy);
                        break;
                    default:
                        imageView.setImageResource(R.drawable.jcz_dengyu);
                        break;
                }
                break;
            case 2:
                imageView.setImageResource(R.drawable.icon_jcwc);
                break;
            case 4:
                imageView.setImageResource(R.drawable.collect_fail);
                break;
            case 5:
                imageView.setImageResource(R.drawable.jmz_qt);
                break;
            case 6:
                switch (entity.getPriceWarning()) {//价格预警 1 大于 2 等于 3 小于
                    case 1:
                        imageView.setImageResource(R.drawable.bjz_dy);
                        break;
                    case 2:
                        imageView.setImageResource(R.drawable.bjz_dengyu);
                        break;
                    case 3:
                        imageView.setImageResource(R.drawable.bjz_xy);
                        break;
                }
//                imageView.setImageResource(R.drawable.icon_bjz);
                break;
            default:
                imageView.setImageResource(R.drawable.icon_jcz);
                break;
        }
    }

    private void updateGroupBuyType(TextView textView, CentralizedMiningEntity entity) {
        switch (entity.getGroupBuyType()) {//集采类型: 1即报集采 2 汇总集采  3.企业专项
            case 1:
                textView.setText(textView.getResources().getText(R.string.jbjc));
                textView.setBackgroundResource(R.drawable.shape_blue_button);
                break;
            case 2:
                textView.setText(textView.getResources().getText(R.string.hzcg));
                textView.setBackgroundResource(R.drawable.shape_yellow_button);
                break;
            case 3:

                if ("1".equals(entity.getOfferType())) {
                    textView.setBackgroundResource(R.drawable.hybg);
                    textView.setText("客户报价");
                } else {
                    textView.setBackgroundResource(R.drawable.shape_bule_green_button);
                    textView.setText("市场报价");
                }
                break;
            default:
                textView.setVisibility(View.GONE);
                break;
        }
    }

    //第二类型标签     //报价类型: 1：商品材料   2：机械租赁  3：设备租赁
    private void updateSecondlyType(TextView tvSecondlyType, CentralizedMiningEntity entity) {
        switch (entity.getGroupBuyClass()) {//报价类型: 0：市场报价   1：会员报价
            case 1:
                tvSecondlyType.setVisibility(View.VISIBLE);
                tvSecondlyType.setText("商品材料");
                tvSecondlyType.setBackgroundResource(R.drawable.shape_blue_button);
                break;
            case 2:
                tvSecondlyType.setVisibility(View.VISIBLE);
                tvSecondlyType.setText("机械租赁");
                tvSecondlyType.setBackgroundResource(R.drawable.shape_purple_button);
                break;
            case 3:
                tvSecondlyType.setVisibility(View.VISIBLE);
                tvSecondlyType.setText("设备租赁");
                tvSecondlyType.setBackgroundResource(R.drawable.shape_green_button);
                break;
            default:
                tvSecondlyType.setVisibility(View.GONE);
                break;
        }
    }

    //报价类型标签
    private void updateQuotationType(TextView tvQuotationType, CentralizedMiningEntity entity) {
        if(entity.getOfferType()==null){
            tvQuotationType.setVisibility(View.GONE);
            return;
        }
        switch (entity.getOfferType()) {//报价类型: 0：市场报价   1：会员报价
            case "1":
                tvQuotationType.setVisibility(View.VISIBLE);
                tvQuotationType.setText("客户报价");
                tvQuotationType.setTextColor(context.getResources().getColor(R.color.color_06C764));
                tvQuotationType.setBackgroundResource(R.drawable.bg_memberoffer);
                break;
            case "2":
                tvQuotationType.setVisibility(View.VISIBLE);
                tvQuotationType.setTextColor(context.getResources().getColor(R.color.color_0286DF));
                tvQuotationType.setText("市场报价");
                tvQuotationType.setBackgroundResource(R.drawable.bg_marketprice);
                break;
            default:
                tvQuotationType.setVisibility(View.GONE);
                break;
        }
    }
    public void setVisible(boolean visible) {
        isVisible = visible;
    }

    public void setItemClickCallBack(ItemClickCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    public interface ItemClickCallBack {
        void onItemClick(int type,String no);
    }

}
